package grep_demo

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"time"
)

func NewClient() {
	// 1. 创建一个连接
	conn, err := grpc.Dial(":8888", grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
		return
	}
	defer conn.Close()

	// 2. 然后创建客户端
	client := NewAddClient(conn)
	now := time.Now()

	for x := 0; x < 10000; x++ {
		// 3. rpc调用
		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
		defer cancel()
		response, err := client.Sum(ctx, &SumRequest{A: 1, B: 2})
		if err != nil {
			fmt.Printf("超时 , 原因 : %s\n", err)
		} else {
			fmt.Printf("结果 : %v\n", response.V)
		}
	}
	fmt.Println(time.Now().Sub(now).Milliseconds())
}
